package com.shenmazong.review.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.review.mapper.*;
import com.shenmazong.review.pojo.*;
import com.shenmazong.review.service.TbUserReviewReportService;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.IdVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeSet;

/**
 *
 */
@Service
@Slf4j
public class TbUserReviewReportServiceImpl extends ServiceImpl<TbUserReviewReportMapper, TbUserReviewReport>
    implements TbUserReviewReportService{

    @Autowired
    TbUserReviewRecMapper tbUserReviewRecMapper;

    @Autowired
    TbAskPaperDetailMapper tbAskPaperDetailMapper;

    @Autowired
    TbUserReviewReportMapper tbUserReviewReportMapper;

    @Autowired
    TbUserReviewMapper tbUserReviewMapper;

    @Autowired
    TbAnswerMapper tbAnswerMapper;

    @Autowired
    TbRiskMapper tbRiskMapper;

    @Autowired
    TbDiseaseMapper tbDiseaseMapper;

    @Autowired
    TbUserReviewDiseaseMapper tbUserReviewDiseaseMapper;

    @Override
    public ResultResponse doMakeReviewReport(IdVo idVo) {
        //
        TbUserReview tbUserReview = tbUserReviewMapper.selectById(idVo.getId());

        //--1 获取用户评测答案的列表 ： tb_user_review_rec : review_id
        List<TbUserReviewRec> tbUserReviewRecs = tbUserReviewRecMapper.selectList(
                new QueryWrapper<TbUserReviewRec>().lambda().eq(TbUserReviewRec::getReviewId, idVo.getId())
        );

        //--2 paper_id\question_id\answer_id
        // 计算分数
        Integer userReviewScore = 0;
        for (int idx = 0; idx < tbUserReviewRecs.size(); idx++) {
            TbUserReviewRec tbUserReviewRec = tbUserReviewRecs.get(idx);
            TbAskPaperDetail tbAskPaperDetail = tbAskPaperDetailMapper.selectOne(
                    new QueryWrapper<TbAskPaperDetail>().lambda()
                            .eq(TbAskPaperDetail::getPaperId, tbUserReviewRec.getPaperId())
                            .eq(TbAskPaperDetail::getQuestionId, tbUserReviewRec.getQuestionId())
                            .eq(TbAskPaperDetail::getAnswerId, tbUserReviewRec.getAnswerId())
                            .last("limit 1")
            );
            userReviewScore += tbAskPaperDetail.getRiskScore();
        }
        log.info("userId:score="+userReviewScore);
        TbUserReviewReport tbUserReviewReport = new TbUserReviewReport();
        tbUserReviewReport.setReviewId(idVo.getId());
        tbUserReviewReport.setUserId(tbUserReview.getUserId());
        tbUserReviewReport.setHealthScore(userReviewScore);
        tbUserReviewReportMapper.insert(tbUserReviewReport);

        //--3 factor_id : 来源于答案表
        ArrayList<Integer> factors = new ArrayList<>();
        for (int idx = 0; idx < tbUserReviewRecs.size(); idx++) {
            TbUserReviewRec tbUserReviewRec = tbUserReviewRecs.get(idx);
            TbAnswer tbAnswer = tbAnswerMapper.selectById(tbUserReviewRec.getAnswerId());
            factors.add(tbAnswer.getFactorId());
        }

        //--4 disease_id
        TreeSet<Integer> diseases = new TreeSet<>();
        for (int idx = 0; idx < factors.size(); idx++) {
            Integer factor = factors.get(idx);
            List<TbRisk> tbRisks = tbRiskMapper.selectList(
                    new QueryWrapper<TbRisk>().lambda().eq(TbRisk::getFactorId, factor)
            );
            for (int ndx = 0; ndx < tbRisks.size(); ndx++) {
                diseases.add(tbRisks.get(ndx).getDiseaseId());
            }
        }

        //--5 把报告写入数据库中
        diseases.stream().forEach(item -> {
            TbDisease tbDisease = tbDiseaseMapper.selectById(item);

            TbUserReviewDisease tbUserReviewDisease = new TbUserReviewDisease();
            tbUserReviewDisease.setReviewId(idVo.getId());
            tbUserReviewDisease.setUserId(tbUserReview.getUserId());
            tbUserReviewDisease.setDiseaseId(tbDisease.getDiseaseId());
            tbUserReviewDisease.setDiseaseName(tbDisease.getDiseaseName());
            //tbUserReviewDisease.setRiskLevel();

            tbUserReviewDiseaseMapper.insert(tbUserReviewDisease);
        });

        // 更新评测状态
        tbUserReview.setReviewStatus(2);
        tbUserReviewMapper.updateById(tbUserReview);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse getUserReviewReportList(IdVo idVo) {
        List<TbUserReview> tbUserReviews = tbUserReviewMapper.selectList(
                new QueryWrapper<TbUserReview>().lambda()
                        .eq(TbUserReview::getUserId, idVo.getId())
                        .eq(TbUserReview::getReviewStatus, 2)
        );

        return ResultResponse.SUCCESS(tbUserReviews);
    }

    @Override
    public ResultResponse getUserReviewReport(IdVo idVo) {

        //--1 获取健康分数
        TbUserReviewReport tbUserReviewReport = tbUserReviewReportMapper.selectOne(
                new QueryWrapper<TbUserReviewReport>().lambda().eq(TbUserReviewReport::getReviewId, idVo.getId())
        );

        //--2 获取疾病风险列表
        List<TbUserReviewDisease> tbUserReviewDiseases = tbUserReviewDiseaseMapper.selectList(
                new QueryWrapper<TbUserReviewDisease>().lambda().eq(TbUserReviewDisease::getReviewId, idVo.getId())
        );

        HashMap<String, Object> map = new HashMap<>();
        map.put("report", tbUserReviewReport);
        map.put("diseases", tbUserReviewDiseases);

        return ResultResponse.SUCCESS(map);
    }
}




